﻿#include "CustomSplitter.h"
#include <QWidget>
#include <QVariant>

CustomSplitter::CustomSplitter(QWidget *parent)
    : QSplitter{parent}
{
    setHandleWidth(22);
    //    setOpaqueResize(false);
     connect(this, &CustomSplitter::splitterMoved, this, &CustomSplitter::onSplitterMoved);
}

QSplitterHandle *CustomSplitter::createHandle()
{
    return new CustomSplitterHandle(orientation(), this);
}

void CustomSplitter::onSplitterMoved(int pos,int index)
{
    CustomSplitterHandle *h = qobject_cast<CustomSplitterHandle*>(handle(1));
    h->checkFoldState(pos);
}

CustomSplitterHandle::CustomSplitterHandle(Qt::Orientation orientation, QSplitter *parent)
    : QSplitterHandle(orientation, parent)
{
    QWidget *parentWgt = Q_NULLPTR;
    if(parent) {
        parentWgt = parent->parentWidget();
    }
    m_toggleButton = new QToolButton(parentWgt);
    m_toggleButton->setObjectName("btnFold");

    
    m_toggleButton->setCursor(Qt::PointingHandCursor);
    m_toggleButton->setCheckable(true);
    m_toggleButton->hide();
    connect(m_toggleButton, &QPushButton::clicked, this, &CustomSplitterHandle::onButtonClicked);
}

void CustomSplitterHandle::setBindForm(QWidget *form)
{
    m_pForm = form;
}

bool CustomSplitterHandle::checkFoldState(int pos)
{
    if(m_pForm)
    {
        bool bfold= m_pForm->width() == 0 ||m_pForm->height() == 0;
        if(m_toggleButton->isCheckable()!=bfold)
            m_toggleButton->setChecked(bfold);
        if(!bfold)
            m_nOldPos = pos;
        moveBtn();
        return bfold;
    }
    return false;
}

void CustomSplitterHandle::resizeEvent(QResizeEvent *event)
{
    moveBtn();
    QSplitterHandle::resizeEvent(event);
    
    // 居中放置按钮
    //    if (orientation() == Qt::Horizontal) {
    //        m_toggleButton->move((width() - m_toggleButton->width()) / 2,
    //                           (height() - m_toggleButton->height()) / 2);
    //    } else {
    //        m_toggleButton->move((width() - m_toggleButton->width()) / 2,
    //                           (height() - m_toggleButton->height()) / 2);
    //    }


}

void CustomSplitterHandle::showEvent(QShowEvent *event)
{
    moveBtn();
    QSplitterHandle::showEvent(event);
}

void CustomSplitterHandle::moveEvent(QMoveEvent *event)
{
    moveBtn();
    QSplitterHandle::moveEvent(event);
}

void CustomSplitterHandle::moveBtn()
{
    if(orientation() == Qt::Horizontal){
        QRect rc = this->geometry();
        QRect rt = QRect(rc.left()-18,rc.top()+(rc.height()+100)/2,8,50);
        m_toggleButton->move(rt.topLeft());
    }
    else
    {
        QRect rc = this->geometry();
        QRect rt = QRect(rc.left()+(rc.width()-50)/2,rc.top() -8,8,50);
        m_toggleButton->move(rt.topLeft());
    }
    if(m_pForm)
        m_toggleButton->show();
}

void CustomSplitterHandle::onButtonClicked(bool isFold)
{

    if(m_pForm)
    {
        m_nMinWidth = m_pForm->parentWidget()->minimumWidth();
        m_pForm->setMinimumWidth(0);
        if(isFold)
        {
            int nPos =0;
            if(orientation() ==Qt::Horizontal)
            {
                if(pos().x() != nPos)
                {
                    m_nOldPos = pos().x();
                }
                if(m_pForm->pos().x() > m_nOldPos)
                {
                    nPos = m_pForm->geometry().right();
                }
            }
            else
            {
                if(pos().y() != nPos)
                {
                    m_nOldPos = pos().y();
                }
                if(m_pForm->pos().y() > m_nOldPos)
                {
                    nPos = m_pForm->geometry().bottom();
                }
            }
            moveSplitter(nPos);
        }
        else
        {
            moveSplitter(m_nOldPos);
        }
        moveBtn();

    }
    //  QSplitter* splitter = qobject_cast<QSplitter*>(parentWidget());
    //    if (!splitter) return;

    //    // 获取分割器右侧窗口的索引
    //    int idx = splitter->indexOf(this) + 1;
    //    if (idx >= splitter->count()) return;

    //    QWidget* widget = splitter->widget(idx);
    //    if (!widget) return;

    //    if (widget->isVisible()) {
    //        // 保存当前尺寸
    //        widget->setProperty("lastSize", widget->size());
    //        widget->hide();
    //        m_toggleButton->setText("<");
    //    } else {
    //        widget->show();
    //        // 恢复之前的尺寸
    //        QSize lastSize = widget->property("lastSize").toSize();
    //        if (lastSize.isValid()) {
    //            QList<int> sizes = splitter->sizes();
    //            sizes[idx] = lastSize.width();
    //            splitter->setSizes(sizes);
    //        }
    //        m_toggleButton->setText(">");
    //    }
}
